package com.oly.cms.admin.service.impl;

import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import com.oly.cms.admin.mapper.CmsTagMapper;
import com.oly.cms.admin.service.ICmsTagService;
import com.oly.cms.common.constant.CmsCacheConstant;
import com.oly.cms.common.domain.VisibleParam;
import com.oly.cms.common.domain.entity.CmsTag;
import com.oly.cms.common.enums.CommonVisibleEnums;
import com.oly.cms.common.event.CacheWebRefreshAllEvent;
import com.oly.cms.general.mapper.TagSearchMapper;
import com.grace.common.constant.UserConstants;
import com.grace.common.exception.ServiceException;

@Service
public class CmsTagServiceImpl implements ICmsTagService {

    @Autowired
    private CmsTagMapper cmsTagMapper;

    @Autowired
    private TagSearchMapper tagSearchMapper;

    @Autowired
    private ApplicationEventPublisher app;

    @Override
    public int insertCmsTag(CmsTag cmsTag) {
        int re = cmsTagMapper.insertCmsTag(cmsTag);
        app.publishEvent(new CacheWebRefreshAllEvent(this, CmsCacheConstant.TAGS_CACHE_KEY_PREFIX));
        return re;

    }

    @Override
    public int insertCmsTags(List<CmsTag> cmsTags) {
        int re = cmsTagMapper.insertCmsTags(cmsTags);
        app.publishEvent(new CacheWebRefreshAllEvent(this, CmsCacheConstant.TAGS_CACHE_KEY_PREFIX));
        return re;

    }

    @Override
    public int deleteCmsTagByIds(Long[] tagIds) {
        int successNum = 0;
        List<Long> failIds = new LinkedList<Long>();
        for (Long tagId : tagIds) {
            if (cmsTagMapper.deleteCmsTagById(tagId) == 1) {
                successNum += 1;
            } else {
                failIds.add(tagId);
            }
        }
        if (tagIds.length != successNum) {
            throw new ServiceException(
                    "删除未完全完成!删除数据" + tagIds.length + "条,其中删除成功" + successNum + "条,其中" + failIds.size()
                            + "条可能由于关联文章不允许删除,未删除的标签ID为:" + failIds);
        }
        app.publishEvent(new CacheWebRefreshAllEvent(this, CmsCacheConstant.TAGS_CACHE_KEY_PREFIX));
        return successNum;
    }

    @Override
    public int updateCmsTagById(CmsTag cmsTag) {
        int re = cmsTagMapper.updateCmsTagById(cmsTag);
        app.publishEvent(new CacheWebRefreshAllEvent(this, CmsCacheConstant.TAGS_CACHE_KEY_PREFIX));
        return re;

    }

    @Override
    public CmsTag selectCmsTagById(long tagId) {
        return tagSearchMapper.selectCmsTagById(tagId);
    }

    @Override
    public boolean checkTagUnique(CmsTag cmsTag) {
        CmsTag checkTag = tagSearchMapper.checkTagUnique(cmsTag);
        Long tagId = cmsTag.getTagId() == null ? -1L : cmsTag.getTagId();
        if (checkTag == null || checkTag.getTagId().longValue() == tagId.longValue()) {
            return UserConstants.UNIQUE;
        }
        return UserConstants.NOT_UNIQUE;
    }

    @Override
    public List<CmsTag> listCmsTagByArticleId(VisibleParam vParam) {
        return tagSearchMapper.listCmsTagByArticleId(vParam);
    }

    @Override
    public List<CmsTag> listCmsTagByTag(CmsTag cmsTag) {
        return tagSearchMapper.listCmsTags(cmsTag);
    }

    @Override
    public int deleteCmsTagById(long tagId) {
        int re = cmsTagMapper.deleteCmsTagById(tagId);
        app.publishEvent(new CacheWebRefreshAllEvent(this, CmsCacheConstant.TAGS_CACHE_KEY_PREFIX));
        return re;
    }

    @Override
    public int batchVisible(Long[] tagIds, CommonVisibleEnums visibleEnum, String username) {
        CmsTag cmsTag = new CmsTag();
        cmsTag.setUpdateBy(username);
        cmsTag.setVisible(visibleEnum.ordinal());
        for (int index = 0; index < tagIds.length; index++) {
            cmsTag.setTagId(tagIds[index]);
            this.updateCmsTagById(cmsTag);
        }
        return tagIds.length;
    }

    @Override
    public int batchOrderNum(Long[] tagIds, long orderNum, String username) {
        CmsTag cmsTag = new CmsTag();
        cmsTag.setUpdateBy(username);
        cmsTag.setOrderNum(orderNum);
        for (int index = 0; index < tagIds.length; index++) {
            cmsTag.setTagId(tagIds[index]);
            this.updateCmsTagById(cmsTag);
        }
        return tagIds.length;
    }

}
